package com.xysq.fragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.action.impl.AppActionImpl;
import com.rockcent.model.CommentModel;
import com.rockcent.model.EvaluateBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.XYApplication;
import com.xysq.adapter.EvaluateAdapter;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class EvaluateFragment extends BaseFragment {
    private static final String couponModelId = "couponModelId";
    private static final int SAME_NUMBER = 100;

    private int id;
    private int maxCommentNumber;
    public AppAction appAction;
    int oneStar = 0;
    int twoStar = 0;
    int threeStar = 0;
    int fourStar = 0;
    int fiveStar = 0;
    ListView evaluateList;
    View header;
    EvaluateAdapter evaluateAdapter;
    List<EvaluateBO> evaluateBOList;
    EvaluateBO evaluateBO;
    ImageLoader imageLoader;
    ImageView twoStarImg;
    ImageView threeStarImg;
    ImageView fourStarImg;
    ImageView fiveStarImg;
    ImageView oneStarImg;
    TextView oneStarTxt;
    TextView twoStarTxt;
    TextView threeStarTxt;
    TextView fourStarTxt;
    TextView fiveStarTxt;
    TextView oneStarWord;
    TextView twoStarWord;
    TextView threeStarWord;
    TextView fourStarWord;
    TextView fiveStarWord;
    @InjectView(R.id.txt_null_data)
    TextView nullDataTxt;
    int currentPage = 1;

    private OnFragmentInteractionListener mListener;

    public static EvaluateFragment newInstance(int mCouponModelId) {
        EvaluateFragment fragment = new EvaluateFragment();
        Bundle args = new Bundle();
        args.putInt(couponModelId, mCouponModelId);
        fragment.setArguments(args);
        return fragment;
    }

    public EvaluateFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            id = getArguments().getInt(couponModelId);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflaterView(inflater, R.layout.fragment_evaluate, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        imageLoader = ((XYApplication) getActivity().getApplication()).getImageLoader();
        loadData();
    }

    private void loadData() {
        getComments();
    }

    private void getComments() {
        appAction.listComment(id, currentPage, 50, new CallbackListener<List<CommentModel>>() {
            @Override
            public void onSuccess(List<CommentModel> data) {
                evaluateBOList.clear();
                int size = data.size();
                if (data.size() == 0) {
                    nullDataTxt.setVisibility(View.VISIBLE);
                } else {
                    for (int i = 0; i < size; i++) {
                        evaluateBO = new EvaluateBO();
                        if (data.get(i) != null) {
                            evaluateBO.setContent(data.get(i).getContent());
                            String name = UseUtil.selectName(data.get(i).getCommentNickName(), data.get(i).getCommentUserName(), data.get(i).getCommentUserPhoneNum());
                            evaluateBO.setCommentUserName(name);
                            evaluateBO.setScore(data.get(i).getScore());
                            evaluateBO.setCreateTime(data.get(i).getCreateTime());
                            collectScore(data.get(i).getScore());
                            evaluateBO.setSchoolName(data.get(i).getSchoolName());
                            evaluateBO.setHeadimgurl(data.get(i).getHeadimgurl());
                        }
                        evaluateBOList.add(evaluateBO);
                    }
                    handleUI();
                    evaluateAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {
                Log.e("++++++++++" + errorCode, message);
                ToastUtil.showShort(getActivity(), "获取评论失败");
            }
        });
    }

    private void handleUI() {
        oneStarTxt.setText(String.valueOf(oneStar));
        twoStarTxt.setText(String.valueOf(twoStar));
        threeStarTxt.setText(String.valueOf(threeStar));
        fourStarTxt.setText(String.valueOf(fourStar));
        fiveStarTxt.setText(String.valueOf(fiveStar));
        dealWithProcessBar();
    }

    private void collectScore(int score) {
        switch (score) {
            case 1:
                oneStar++;
                break;
            case 2:
                twoStar++;
                break;
            case 3:
                threeStar++;
                break;
            case 4:
                fourStar++;
                break;
            case 5:
                fiveStar++;
                break;
            default:
                break;
        }
    }

    private void initView() {
        header = View.inflate(getActivity(), R.layout.evaluate_head, null);
        evaluateList = (ListView) getView().findViewById(R.id.list_evaluate);
        oneStarTxt = (TextView) header.findViewById(R.id.txt_one_star);
        twoStarTxt = (TextView) header.findViewById(R.id.txt_two_star);
        threeStarTxt = (TextView) header.findViewById(R.id.txt_three_star);
        fourStarTxt = (TextView) header.findViewById(R.id.txt_four_star);
        fiveStarTxt = (TextView) header.findViewById(R.id.txt_five_star);
        oneStarWord = (TextView) header.findViewById(R.id.txt_one_word);
        twoStarWord = (TextView) header.findViewById(R.id.txt_two_word);
        threeStarWord = (TextView) header.findViewById(R.id.txt_three_word);
        fourStarWord = (TextView) header.findViewById(R.id.txt_four_word);
        fiveStarWord = (TextView) header.findViewById(R.id.txt_five_word);
        oneStarImg = (ImageView) header.findViewById(R.id.img_one_star);
        twoStarImg = (ImageView) header.findViewById(R.id.img_two_star);
        threeStarImg = (ImageView) header.findViewById(R.id.img_three_star);
        fourStarImg = (ImageView) header.findViewById(R.id.img_four_star);
        fiveStarImg = (ImageView) header.findViewById(R.id.img_five_star);
        appAction = AppActionImpl.getInstance(getActivity(), PropertiesUtil.getXyapiAddress(), PropertiesUtil.getQkapiAddress());
        evaluateBOList = new ArrayList<>();
        evaluateList.addHeaderView(header);
        evaluateAdapter = new EvaluateAdapter(getActivity(), evaluateBOList, imageLoader);
        evaluateList.setAdapter(evaluateAdapter);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void dealWithProcessBar() {
        showProcess(oneStar, oneStarImg);
        showProcess(twoStar, twoStarImg);
        showProcess(threeStar, threeStarImg);
        showProcess(fourStar, fourStarImg);
        showProcess(fiveStar, fiveStarImg);
        List<Integer> maxList = new ArrayList<Integer>();
        maxList = findMax(oneStar, twoStar, threeStar, fourStar, fiveStar);
        processMaxColor(maxList);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void processMaxColor(int max) {
        if (isAdded()) {
            switch (max) {
                case 1:
                    oneStarTxt.setTextColor(getResources().getColor(R.color.text_red));
                    oneStarWord.setTextColor(getResources().getColor(R.color.text_red));
                    giveColorRed(maxCommentNumber, oneStarImg);
                    break;
                case 2:
                    twoStarTxt.setTextColor(getResources().getColor(R.color.text_red));
                    twoStarWord.setTextColor(getResources().getColor(R.color.text_red));
                    giveColorRed(maxCommentNumber, twoStarImg);
                    break;
                case 3:
                    threeStarTxt.setTextColor(getResources().getColor(R.color.text_red));
                    threeStarWord.setTextColor(getResources().getColor(R.color.text_red));
                    giveColorRed(maxCommentNumber, threeStarImg);
                    break;
                case 4:
                    fourStarTxt.setTextColor(getResources().getColor(R.color.text_red));
                    fourStarWord.setTextColor(getResources().getColor(R.color.text_red));
                    giveColorRed(maxCommentNumber, fourStarImg);
                    break;
                case 5:
                    fiveStarTxt.setTextColor(getResources().getColor(R.color.text_red));
                    fiveStarWord.setTextColor(getResources().getColor(R.color.text_red));
                    giveColorRed(maxCommentNumber, fiveStarImg);
                    break;
                default:
                    break;
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void giveColorRed(int maxCommentNumber, ImageView img) {
        switch (maxCommentNumber) {
            case 1:
                img.setBackground(getResources().getDrawable(R.drawable.bar1));
                break;
            case 2:
                img.setBackground(getResources().getDrawable(R.drawable.bar2));
                break;
            case 3:
                img.setBackground(getResources().getDrawable(R.drawable.bar3));
                break;
            case 4:
                img.setBackground(getResources().getDrawable(R.drawable.bar4));
                break;
            case 5:
                img.setBackground(getResources().getDrawable(R.drawable.bar5));
                break;
            case 6:
                img.setBackground(getResources().getDrawable(R.drawable.bar6));
                break;
            case 7:
                img.setBackground(getResources().getDrawable(R.drawable.bar7));
                break;
            case 8:
                img.setBackground(getResources().getDrawable(R.drawable.bar8));
                break;
            case 9:
                img.setBackground(getResources().getDrawable(R.drawable.bar9));
                break;
            case 10:
                img.setBackground(getResources().getDrawable(R.drawable.bar10));
                break;
            default:
                img.setBackground(getResources().getDrawable(R.drawable.bar10));
                break;
        }
    }

    private void processMaxColor(List<Integer> maxList) {
        int size = maxList.size();
        for (int i = 0; i < size; i++) {
//            giveColor(i);
            processMaxColor(maxList.get(i));
        }
    }

    private List<Integer> findMax(int oneStar, int twoStar, int threeStar, int fourStar, int fiveStar) {
        int[] fiveNumber = {oneStar, twoStar, threeStar, fourStar, fiveStar};
        List<Integer> maxList = new ArrayList<Integer>();
        maxCommentNumber = oneStar;
        for (int i = 1; i < 5; i++) {
            if (maxCommentNumber < fiveNumber[i]) {
                maxCommentNumber = fiveNumber[i];
            }
        }
        if (maxCommentNumber != 0) {
            for (int i = 0; i < 5; i++) {
                if (maxCommentNumber == fiveNumber[i]) {
                    maxList.add(i + 1);
                }
            }
        }
        return maxList;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void showProcess(int star, ImageView img) {
        if (isAdded()) {
            switch (star) {
                case 0:
                    img.setBackground(null);
                    break;
                case 1:
                    img.setBackground(getResources().getDrawable(R.drawable.bar1_unselected));
                    break;
                case 2:
                    img.setBackground(getResources().getDrawable(R.drawable.bar2_unselected));
                    break;
                case 3:
                    img.setBackground(getResources().getDrawable(R.drawable.bar3_unselected));
                    break;
                case 4:
                    img.setBackground(getResources().getDrawable(R.drawable.bar4_unselected));
                    break;
                case 5:
                    img.setBackground(getResources().getDrawable(R.drawable.bar5_unselected));
                    break;
                case 6:
                    img.setBackground(getResources().getDrawable(R.drawable.bar6_unselected));
                    break;
                case 7:
                    img.setBackground(getResources().getDrawable(R.drawable.bar7_unselected));
                    break;
                case 8:
                    img.setBackground(getResources().getDrawable(R.drawable.bar8_unselected));
                    break;
                case 9:
                    img.setBackground(getResources().getDrawable(R.drawable.bar9_unselected));
                    break;
                case 10:
                    img.setBackground(getResources().getDrawable(R.drawable.bar10_unselected));
                    break;
                default:
                    img.setBackground(getResources().getDrawable(R.drawable.bar10_unselected));
                    break;
            }
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(Uri uri);
    }

    @Override
    public void onPause() {
        MobclickAgent.onPageEnd("商品技能评价页");
        super.onPause();
    }

    @Override
    public void onResume() {
        MobclickAgent.onPageStart("商品技能评价页");
        super.onResume();
    }
}
